
package com.weiyuwqj.admin.controller.admin.invest;

import com.weiyuwqj.admin.controller.admin.log.annotation.LogAnnotation;
import com.weiyuwqj.admin.controller.base.BaseController;
import com.weiyuwqj.api.service.SysContractsService;
import com.weiyuwqj.api.service.BusInvestRepayService;
import com.weiyuwqj.api.service.BusInvestService;
import com.weiyuwqj.api.common.Paginator;
import com.weiyuwqj.api.constant.Message;
import com.weiyuwqj.api.model.convert.InvestAddtimeConvert;
import com.weiyuwqj.api.model.convert.InvestRepayStatusCovert;
import com.weiyuwqj.api.model.convert.ModelConvert;
import com.weiyuwqj.api.model.vo.BusItemRepayVo;
import com.weiyuwqj.api.util.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by yxw on 2015/12/28.
 */
@Controller
@RequestMapping(value = "/admin/invest")
public class BusInvestRepayController extends BaseController {
    @Autowired
    BusInvestRepayService repayService;
    @Autowired
    BusInvestService investService;
    @Autowired
    SysContractsService contractsService;


    /**
     * 显示&查询
     *
     * @param paginator
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/repayList", method = RequestMethod.POST)
    public Object repayList(Paginator paginator, HttpServletRequest request) {
        String itemname = request.getParameter("itemname");
        String name = request.getParameter("name");
        String mobile = request.getParameter("mobile");
        String userId = request.getParameter("userId");
        String repayStatus = request.getParameter("repayStatus");
        String repayStartTime = request.getParameter("repayStartTime");
        String repayEndTime = request.getParameter("repayEndTime");
        String actualStartTime = request.getParameter("actualStartTime");
        String actualEndTime = request.getParameter("actualEndTime");
        String status = request.getParameter("status"); //状态
        String guaranteeUserName = request.getParameter("guaranteeUserName"); //状态
        String orderName = request.getParameter("orderName");
        String orderWay = request.getParameter("orderWay");
        Map<String, Object> map = new HashMap<String, Object>();
        if (repayStatus != "" && repayStatus != null) {
            map.put("repayStatus", repayStatus);
        }
        map.put("itemname", itemname);
        map.put("userId", userId);
        map.put("name", name);
        map.put("mobile", mobile);
        map.put("guaranteeUserName", guaranteeUserName);
        map.put("repayStartTime", repayStartTime);
        map.put("repayEndTime", repayEndTime);
        map.put("actualStartTime", actualStartTime);
        map.put("actualEndTime", actualEndTime);
        map.put("status", status);
        map.put("orderName", orderName);
        map.put("orderWay", orderWay);
        return repayService.getList(paginator, map);
    }


    //老系统投资生成合同
    @ResponseBody
    @RequestMapping("/deleteContract")
    public Object deleteContract(Integer contractId) {
        contractsService.delete(contractId);
        return Message.OK;
    }

    /**
     * 查看合同列表
     *
     * @param paginator
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/listByItemId", method = RequestMethod.POST)
    public Object listByItemId(Paginator paginator, Integer itemId) {
        Map map = new HashMap();
        map.put("itemId", itemId);
        return contractsService.listByItemId(paginator, map);
    }

    /**
     * 导出excel
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/excel/repay")
    public Object repayExcel(HttpServletRequest request,
                             String userId,
                             String itemname,
                             String name,
                             String mobile,
                             String repayStatus,
                             String repayStartTime,
                             String repayEndTime,
                             String actualStartTime,
                             String actualEndTime,
                             String guaranteeUserName,
                             String status) {

        String[] cArray = {"项目名称", "担保公司", "用户名", "真实姓名", "项目id", "还款状态", "当前期数", "总期数", "应还总额", "应还本金", "应还利息", "应还时间", "已收总额", "已收本金", "已收利息", "已还时间"};
        String[] eArray = {"ItemName", "GuaranteeUserName", "Username", "Realname", "ItemId", "Status", "RepayCurrentPeriod", "RepayTotalPeriod", "RepayAmount",
                "RepayPrincipal", "RepayInterest", "RepayTime", "ActualRepayAmount", "ActualRepayPrincipal", "ActualRepayInterest", "ActualRepayTime"};
        ModelConvert[] converts = {null, null, null, null, null, new InvestRepayStatusCovert(), null, null, null, null, null, new InvestAddtimeConvert(), null, null, null, new InvestAddtimeConvert()};

        List<String> cList = Arrays.asList(cArray);
        List<String> eList = Arrays.asList(eArray);
        List<ModelConvert> convertList = Arrays.asList(converts);
        Map map = new HashMap();
        map.put("userId", userId);
        map.put("itemname", itemname);
        map.put("name", name);
        map.put("mobile", mobile);
        map.put("repayStatus", repayStatus);
        map.put("guaranteeUserName", guaranteeUserName);
        map.put("actualStartTime", actualStartTime);
        map.put("repayStartTime", repayStartTime);
        map.put("repayEndTime", repayEndTime);
        map.put("actualEndTime", actualEndTime);
        map.put("status", status);

        List<BusItemRepayVo> contentList = repayService.getList(map);

        String serverDir = request.getServletContext().getRealPath("/") + "admin/export/";
        String excelFilePath = ExcelUtils.export("还款计划", cList, eList, convertList, contentList, serverDir);
        HashMap ret = Message.OK;
        ret.put("file", "export/" + excelFilePath);
        return ret;
    }

    /**
     * 投资页面还款计划列表
     */
    @ResponseBody
    @RequestMapping(value = "/getRepayList", method = RequestMethod.POST)
    public Object getRepayList(String itemId) {
        Map map = new HashMap();
        map.put("itemId", itemId);
        return repayService.getRepayList(map);
    }

    /**
     * 统计列表
     */
    @ResponseBody
    @RequestMapping("/statRepay")
    public Object statRepay(HttpServletRequest request) {
        String itemname = request.getParameter("itemname");
        String name = request.getParameter("name");
        String mobile = request.getParameter("mobile");
        String userId = request.getParameter("userId");
        String repayStatus = request.getParameter("repayStatus");
        String repayStartTime = request.getParameter("repayStartTime");
        String repayEndTime = request.getParameter("repayEndTime");
        String actualStartTime = request.getParameter("actualStartTime");
        String actualEndTime = request.getParameter("actualEndTime");
        String status = request.getParameter("status"); //状态
        String orderName = request.getParameter("orderName");
        String orderWay = request.getParameter("orderWay");
        Map<String, Object> map = new HashMap<String, Object>();
        if (repayStatus != "" && repayStatus != null) {
            map.put("repayStatus", repayStatus);
        }
        map.put("itemname", itemname);
        map.put("userId", userId);
        map.put("name", name);
        map.put("mobile", mobile);
        map.put("repayStartTime", repayStartTime);
        map.put("repayEndTime", repayEndTime);
        map.put("actualStartTime", actualStartTime);
        map.put("actualEndTime", actualEndTime);
        map.put("status", status);
        map.put("orderName", orderName);
        map.put("orderWay", orderWay);
        Map statRepayMap = repayService.statRepay(map);
        Map statNoRepayMap = repayService.statNoRepay(map);
        statRepayMap.putAll(statNoRepayMap);
        return statRepayMap;
    }


    /**
     * 根据guaranteeid统计
     */
    @ResponseBody
    @RequestMapping("/statRepayByGuarantee")
    public Object statRepayByGuarantee(HttpServletRequest request) {
        int guaranteeUserId = this.getSessionUserId();
        String itemname = request.getParameter("itemname");
        String name = request.getParameter("name");
        String mobile = request.getParameter("mobile");
        String userId = request.getParameter("userId");
        String repayStatus = request.getParameter("repayStatus");
        String repayStartTime = request.getParameter("repayStartTime");
        String repayEndTime = request.getParameter("repayEndTime");
        String actualStartTime = request.getParameter("actualStartTime");
        String actualEndTime = request.getParameter("actualEndTime");
        String status = request.getParameter("status"); //状态
        String orderName = request.getParameter("orderName");
        String orderWay = request.getParameter("orderWay");
        Map<String, Object> map = new HashMap<String, Object>();
        if (repayStatus != "" && repayStatus != null) {
            map.put("repayStatus", repayStatus);
        }
        map.put("itemname", itemname);
        map.put("guaranteeUserId", guaranteeUserId);
        map.put("userId", userId);
        map.put("name", name);
        map.put("mobile", mobile);
        map.put("repayStartTime", repayStartTime);
        map.put("repayEndTime", repayEndTime);
        map.put("actualStartTime", actualStartTime);
        map.put("actualEndTime", actualEndTime);
        map.put("status", status);
        map.put("orderName", orderName);
        map.put("orderWay", orderWay);
        Map statRepayMap = repayService.statRepay(map);
        Map statNoRepayMap = repayService.statNoRepay(map);
        statRepayMap.putAll(statNoRepayMap);
        return statRepayMap;
    }

//    //借款人还款
//    @ResponseBody
//    @RequestMapping("/borrowerRepay")
//    public Object borrowerRepay(Integer itemId, Integer currentPeriod) {
//        return repayService.borrowerRepay(itemId, currentPeriod);
//    }
//
//    //标还款给投资人
//    @ResponseBody
//    @RequestMapping("/itemRepay")
//    public Object itemRepay(Integer itemId, Integer currentPeriod) {
//        return repayService.itemRepay(itemId, currentPeriod);
//    }


}

